﻿using Bouyei.Geo.Geometries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouyei.Geo.GeoParsers.GeoJson
{
    public class GeometryReader : BaseString
    {
        public GeoType GeometryType { get;private set; }

        public GeometryReader(string geoJson)
        : base(geoJson) { }

        public Geometry Reader()
        {
            if (Content.Contains("MultiPolygon"))
            {
                var geo = new GeoJsonMultiPolygon(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else if (Content.Contains("Polygon"))
            {
                var geo = new GeoJsonPolygon(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else if (Content.Contains("MultiLineString"))
            {
                var geo = new GeoJsonMultiLineString(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else if (Content.Contains("LineString"))
            {
                var geo = new GeoJsonLineString(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else if (Content.Contains("MultiPoint"))
            {
                var geo = new GeoJsonMultiPoint(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else if (Content.Contains("Point"))
            {
                var geo = new GeoJsonPoint(Content).ToGeometry();
                GeometryType = geo.GeometryType;
                return geo;
            }
            else
            {
                throw new Exception("unrecognized geometry type");
            }
        }

        public object Reader<T>() where T:class
        {
            FeatureCollection<T> f = new FeatureCollection<T>();
            if (this.Content.Contains("MultiPoint"))
                return f.FromGeoJsonMultiPoint(this.Content);
            else if (this.Content.Contains("Point"))
                return f.FromGeoJsonPoint(this.Content);
            else if (this.Content.Contains("MultiLineString"))
                return f.FromGeoJsonMultiLineString(this.Content);
            else if (this.Content.Contains("LineString"))
                return f.FromGeoJsonLineString(this.Content);
            else if (this.Content.Contains("MultiPolygon"))
                return f.FromGeoJsonMultiPolygon(this.Content);
            else if (this.Content.Contains("Polygon"))
                return f.FromGeoJsonPolygon(this.Content);
            else
                throw new Exception("not found geotype");
        }
    }
}
